package option;

import model.Author;
import model.GetAuthors_Optional;
import model.GetAuthors_Optional_null;

import java.util.Optional;

public class OfElseGet {
    public static void main1(String[] args) {
        //值不为空
        Optional<Author> author = GetAuthors_Optional.getAuthor();
        Author author1 = author.orElseGet(() -> new Author("备胎"));
        System.out.println(author1.getName()+" ：working");

        //值为空
        Optional<Author> author2 = GetAuthors_Optional_null.getAuthor();
        Author author3 = author2.orElseGet(() -> new Author("备胎"));
        System.out.println(author3.getName()+": working");
    }

    public static void main2(String[] args) {
        //值不为空
        Optional<Author> author = GetAuthors_Optional.getAuthor();
        Author author1 = author.orElseThrow(()->new RuntimeException("值为空"));
        System.out.println(author1.getName()+" ：working");

        //值为空
        Optional<Author> author2 = GetAuthors_Optional_null.getAuthor();
        Author author3 = author2.orElseThrow(()->new RuntimeException("值为空"));
        System.out.println(author3.getName()+": working");
    }
}
